#include "demo_Fifo.h"
#include "tool_Fifo.h"
#include "sys_Debug.h"
#include "bsp_Inc.h"
#include <string.h>

void demo_fifo()
{
#if DEMO_FIFO
    int i = 0;
    int count = 0;
    QUEUE_DATA_T temp_int = 0;
    QUEUE_DATA_T str[] = "this is a demo of fifo!";
    QUEUE_DATA_T temp_str[23] = {0};
    SysDebug("Fifo Demo Running\r\n");
    while (1)
    {
        SysDebug("1 >> push int to fifo!\r\n");
        for (i = 1; i <= 5; i++)
        {
            demoFifo.push(i);
            count = demoFifo.getCount();
            SysDebug("push %d to fifo,count = %d\r\n", i, count);
            BSP_Delay_ms(1000);
        }
        SysDebug("2 >> pop int from fifo!\r\n");
        for (i = 1; i <= 5; i++)
        {
            demoFifo.pop(&temp_int);
            count = demoFifo.getCount();
            SysDebug("pop %d from fifo,count = %d\r\n", temp_int, count);
            BSP_Delay_ms(1000);
        }
        SysDebug("3 >> push str to fifo!\r\n");
        demoFifo.pushArray(str, 23);
        count = demoFifo.getCount();
        SysDebug("push \"%s\" to fifo,count = %d\r\n", str, count);
        BSP_Delay_ms(1000);

        SysDebug("4 >> pop str from fifo!\r\n");
        demoFifo.popArray(temp_str, 23);
        count = demoFifo.getCount();
        SysDebug("pop \"%s\" from fifo,count = %d\r\n", temp_str, count);
        BSP_Delay_ms(1000);

        SysDebug("5 >>push str to fifo!\r\n");
        demoFifo.pushArray(str, 23);
        count = demoFifo.getCount();
        SysDebug("push \"%s\" to fifo,count = %d\r\n", str, count);
        BSP_Delay_ms(1000);

        SysDebug("6 >>clear fifo!\r\n");
        demoFifo.clear();
        BSP_Delay_ms(1000);

        SysDebug("7 >>try to pop str from fifo!\r\n");
        if (demoFifo.popArray(temp_str, 23) != demoFifo.QUEUE_OK)
        {
            count = demoFifo.getCount();
            SysDebug("fifo is empty!count = %d\r\n", count);
        }
        else
        {
            count = demoFifo.getCount();
            SysDebug("pop \"%s\" from fifo,count = %d\r\n", str, count);
        }
        BSP_Delay_ms(5000);
        SysDebug("loop again!\r\n");
    }

#else
    while (1)
    {
        BSP_Delay_ms(1000);
    }
#endif
}
